package com.meeting.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meeting.annotation.AuthCheck;
import com.meeting.common.result.Result;
import com.meeting.entity.BookingOrder;
import com.meeting.entity.MeetingRoom;
import com.meeting.model.dto.bookingorder.BookingOrderQueryRequest;
import com.meeting.model.dto.meetingroom.MeetingRoomQueryRequest;
import com.meeting.model.dto.meetingroom.MeetingRoomStatusModifyRequest;
import com.meeting.model.dto.user.UserLoginRequest;
import com.meeting.model.dto.user.UserRegisterRequest;
import com.meeting.model.enums.UserRole;
import com.meeting.service.BookingOrderService;
import com.meeting.service.MeetingRoomService;
import com.meeting.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

@RestController
@RequestMapping("/employee")
@Tag(name = "员工接口")
public class EmployeeController {

    @Resource
    private UserService userService;

    @Resource
    private MeetingRoomService meetingRoomService;

    @Resource
    private BookingOrderService bookingOrderService;

    @PostMapping("/login")
    @Operation(summary = "员工登录", description = "员工通过账号密码进行登录认证")
    public Result<String> login(@Valid @RequestBody UserLoginRequest userLoginRequest) {
        String token = userService.employeeLogin(userLoginRequest.getUsername(), userLoginRequest.getPassword());
        return Result.success(token);
    }

    @PostMapping("register")
    @Operation(summary = "员工注册", description = "员工通过账号密码进行注册")
    public Result<Boolean> register(@Valid @RequestBody UserRegisterRequest userRegisterRequest) {
        Boolean res = userService.register(userRegisterRequest);
        return Result.success(res); 
    }

    @PutMapping("modify-meeting-room")
    @AuthCheck(mustRole = UserRole.EMPLOYEE)
    @Operation(summary = "员工修改会议室状态", description = "员工修改会议室状态")
    public Result<Boolean> modifyMeetingRoom(@Valid @RequestBody MeetingRoomStatusModifyRequest request) {
        Boolean res = meetingRoomService.modifyMeetingRoomStatus(request);
        return Result.success(res);
    }

    @PostMapping("booking-order")
    @AuthCheck(mustRole = UserRole.EMPLOYEE)
    @Operation(summary = "员工获取所有订单信息", description = "员工获取所有订单信息")
    public Result<Page<BookingOrder>> getBookingOrder(@RequestBody BookingOrderQueryRequest queryRequest) {
        long pageNum = queryRequest.getPageNum();
        long pageSize = queryRequest.getPageSize();
        LambdaQueryWrapper<BookingOrder> wrapper = bookingOrderService.getQueryWrapper(queryRequest);
        Page<BookingOrder> page = bookingOrderService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(page);
    }

    @PutMapping("refund")
    @AuthCheck(mustRole = UserRole.EMPLOYEE)
    @Operation(summary = "员工退款", description = "员工给客户退款")
    public Result<Boolean> refund(@RequestParam("id") Long id) throws ParseException {
        boolean b = bookingOrderService.refund(id);
        return Result.success(b);
    }

    @PutMapping("refuse-refund")
    @AuthCheck(mustRole = UserRole.EMPLOYEE)
    @Operation(summary = "员工拒绝退款", description = "员工拒绝退款")
    public Result<Boolean> refuseRefund(@RequestParam("id") Long id) throws ParseException {
        boolean b = bookingOrderService.refuseRefund(id);
        return Result.success(b);
    }
}